home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_08_02
/
8n02102a
< prev
next >
Wrap
Text File
|
1990-02-28
|
1KB
|
87 lines
*****Listing 1*****
#include <stdio.h>
main(argc,argv)
int argc;
char **argv;
{
int i;
void crc();
if (argc <= 1)
{
printf("USAGE:crc15 filename [filename...]\n");
exit(1);
}
for(i=1; i < argc; i++)
{
printf ("\n%-s ",argv[i]);
crc(argv[i]);
}
exit(0);
} /* main */
/* CRC
* Cycric Redundancy Check
*
*/
void crc(argv)
char *argv;
{
FILE *fd;
int crc;
int c;
char crc_char;
int crc_clear(),crc_update(),crc_finish();
fd = fopen(argv,"rb");
if(!fd)
{
fprintf(stderr,"Can't open %s !\n",argv);
exit(1);
}
crc = crc_clear();
while((c = getc(fd)) != EOF)
{
crc_char = c;
crc = crc_update(crc,crc_char);
}
crc = crc_finish(crc);
printf("%04x",crc);
fclose(fd);
} /* crc */
int crc_clear()
{
return(0);
}
int crc_update(crc,crc_char)
int crc;
char crc_char;
{
long x;
int i;
x = ((long)crc << 8) + crc_char;
for(i = 0;i < 8;i++)
{
x = x << 1;
if(x & 0x01000000)
x = x ^ 0x01A09700;
}
return(((x & 0x00ffff00) >> 8));
}
int crc_finish(crc)
int crc;
{
return(crc_update(crc_update(crc,'\0'),'\0'));
}